/** 
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * 
 *MeetTypeManageController
 * 会议类型
 * 
 * 修改纪录
 * 2020-10-23 版本：1.0 wanghongwei 创建。
 * @version 版本：1.0
 * @author 作者：wanghongwei
 * 创建日期：2020-10-23
 */

package com.topscomm.meet.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.cbo.model.TreeModel;
import com.topscomm.meet.controller.auto.MeetTypeManageControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: wanghongwei
 * @date: 2020-10-23
 * @description:会议类型Controller
 * @modify:
 */
@RestController
@RequestMapping("meet/typeManage")
public class MeetTypeManageController extends MeetTypeManageControllerAuto implements IMeetTypeManageController {

	/**
	 * @author: wanghongwei
	 * @date: 2020-10-23
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_parentid")) {
			String value = paraMap.get("qry_parentid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and parentid = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: wanghongwei
	 * @date: 2020-10-23
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description: 查询会议类型树
	 * @param paraMap
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-10-23 17:28:14
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/queryTreeList")
	public ResponseResult<Object> queryTreeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		String whereSql = this.spellListSql(paraMap);
		whereSql += " order by sortcode asc";
		List<Map<String, Object>> listNewModule = this.service.queryMapByWhere(whereSql);
		TreeModel treeModel = new TreeModel();
		List<TreeModel> meetTypeTreeList = null;
		if (ConvertUtil.convertToString(paraMap.get("isAddRoot")).equals("false")) {
			meetTypeTreeList = treeModel.buildTree(listNewModule, false, "会议类型树");
		} else {
			meetTypeTreeList = treeModel.buildTree(listNewModule, true, "会议类型树");
		}
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("dataList", meetTypeTreeList);
		return ResponseResult.ok(dataMap);
	}

	/**
	 * @description: 查询会议类型
	 * @param paraMap
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-11-24 8:54:06
	 * @modify:
	 */
	@GetMapping(value = "/querySpliceList")
	public ResponseResult<Object> querySpliceList(@RequestParam(required = false) Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		Query query = new Query(paraMap);
		if (query.getCurrentPage() <= 0 || query.getPageSize() <= 0) {
			return ResponseResult.error("缺少分页参数或者分页参数配置错误");
		}
		String whereSql = " 1=1 order by sortcode asc";
		List<Map<String, Object>> meetTypeList = this.service.queryMapByWhere(whereSql);
		// 得到组装的所有list数据（二级）
		List<Map<String, Object>> spliceTypeList = this.service.getSpliceList(meetTypeList);
		List<Map<String, Object>> qryTypeList = new ArrayList<>();
		String qryName = ConvertUtil.convertToString(paraMap.get("qry_name"));
		if (qryName.length() > 0) {
			for (Map<String, Object> map : spliceTypeList) {
				if (ConvertUtil.convertToString(map.get("fullname")).contains(qryName)) {
					qryTypeList.add(map);
				}
			}
		} else {
			qryTypeList.addAll(spliceTypeList);
		}
		int length = qryTypeList.size();
		int startIndex = query.getPageSize() * (query.getCurrentPage() - 1);
		Page page = null;
		if (startIndex >= length) {
			page = new Page(new ArrayList<String>(), length, query.getPageSize(), query.getCurrentPage());
		} else {
			int endIndex = query.getPageSize() * query.getCurrentPage();
			endIndex = endIndex > length ? length : endIndex;
			page = new Page(qryTypeList.subList(startIndex, endIndex), length, query.getPageSize(),
					query.getCurrentPage());
		}
		return ResponseResult.ok(page);
	}
}
